package com.tt5.services.handlers

import com.tt5.services.handlers.user.AuthHandler
import com.tt5.services.handlers.user.UserInfoHandler
import com.tt5.services.vertx.coroutineHandler
import io.vertx.core.Vertx
import io.vertx.ext.auth.JWTOptions
import io.vertx.ext.auth.jwt.JWTAuth
import io.vertx.ext.auth.jwt.JWTAuthOptions
import io.vertx.ext.web.Router
import io.vertx.ext.web.handler.JWTAuthHandler
import io.vertx.ext.web.handler.SessionHandler
import io.vertx.ext.web.sstore.LocalSessionStore

object AppRouter {
  lateinit var sessionHandler: SessionHandler
  lateinit var jwtAuth: JWTAuth

  /**
   * jwt访问Token配置
   */
  lateinit var jwtAccessOpt: JWTOptions

  /**
   * jwt刷新Token配置
   */
  lateinit var jwtRefreshOpt: JWTOptions

  suspend fun create(vertx: Vertx, router: Router) {
    sessionHandler = SessionHandler.create(LocalSessionStore.create(vertx))
    jwtAuth = JWTAuth.create(vertx, JWTAuthOptions())

    router.route().handler(sessionHandler)
    AuthHandler.route(vertx, router)
    router.route("/userinfo").coroutineHandler(UserInfoHandler::get)
  }
}
